package com.community.common.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.community.common.config.BizException;
import com.community.common.mapper.BuildingMapper;
import com.community.common.mapper.CommunityMapper;
import com.community.common.mapper.HouseMapper;
import com.community.common.mapper.OwnerHouseMapper;
import com.community.common.pojo.PageRequest;
import com.community.common.pojo.domain.Building;
import com.community.common.pojo.domain.Community;
import com.community.common.pojo.domain.House;
import com.community.common.pojo.domain.OwnerHouse;
import com.community.common.pojo.dto.CommunityCondition;
import com.community.common.pojo.response.MyResponse;
import com.community.common.util.CommonUtil;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 小区表 服务实现类
 * </p>
 *
 * @author superman
 * @since 2020-08-02
 */
@Slf4j
@Service
public class CommunityService {

    @Resource
    private CommunityMapper communityMapper;
    @Resource
    private BuildingMapper buildingMapper;
    @Resource
    private HouseMapper houseMapper;
    @Resource
    private OwnerHouseMapper ownerHouseMapper;


    public MyResponse add(Community community) {
        community.setId(String.valueOf(CommonUtil.getId()));
        community.setCreateTime(LocalDateTime.now());
        community.setUpdateTime(LocalDateTime.now());
        int insert = communityMapper.insert(community);
        if (insert == 1) {
            return MyResponse.success();
        }
        return MyResponse.fail();
    }

    public boolean delete(List<Long> ids) {

        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }

        for (Long xqId : ids) {
            QueryWrapper<Building> wrapper = new QueryWrapper<>();
            wrapper.eq("xq_id", xqId);
            if (buildingMapper.selectCount(wrapper) > 0) {
                throw new BizException("小区下有楼栋信息，无法删除");
            }
        }

        for (Long xqId : ids) {
            QueryWrapper<OwnerHouse> wrapper = new QueryWrapper<>();
            wrapper.eq("communityId", xqId);
            if (ownerHouseMapper.selectCount(wrapper) > 0) {
                throw new BizException("小区下有人员信息，无法删除");
            }
        }

        if (ids != null && ids.size() > 0) {
            for (Long id : ids) {
                communityMapper.deleteById(id);
                return true;
            }
        }
        return false;
    }

    public boolean edit(Community community) {
        community.setUpdateTime(LocalDateTime.now());
        int i = communityMapper.updateById(community);
        return true;
    }

    public Community getById(Long id) {
        return communityMapper.selectById(id);
    }

    public List<Community> queryByPage(PageRequest<CommunityCondition> pageRequest) {
        CommunityCondition condition = pageRequest.getCondition();
        LambdaQueryWrapper<Community> wrapper = new LambdaQueryWrapper<>();
        if (condition != null) {
            if (StringUtils.isNotBlank(condition.getName())) {
                wrapper.like(Community::getName, condition.getName());
            }
        }
        PageHelper.startPage(pageRequest.getCurrentPage(), pageRequest.getPageSize());
        return communityMapper.selectList(wrapper);
    }

    public List<Community> queryAllCommunity(String openId) {
        return communityMapper.queryAllCommunity(openId);
    }

    public List<Building> queryAllBuilding(String xqId) {
        return buildingMapper.queryAllBuilding(xqId);
    }

    public List<House> queryAllHouse(String ldId) {
        return houseMapper.queryAllHouse(ldId);
    }

    public Integer countByName(String name) {
        LambdaQueryWrapper<Community> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Community::getName, name);
        return communityMapper.selectCount(wrapper);
    }


}
